import _ from 'lodash';

export function min(array: number[]) {
  return _.min(array.filter((n) => typeof n === 'number'));
}
export function max(array: number[]) {
  return _.max(array.filter((n) => typeof n === 'number'));
}
export function sum(array: number[]) {
  return _.sum(array.filter((n) => typeof n === 'number'));
}
export function average(array: number[]) {
  return array.length === 0 ? 0 : _.sum(array) / array.length;
}

export function allEqual(array: any[]) {
  return _.every(array, (value) => value === array[0]);
}
export function isEqual(value: any, other: any) {
  return _.isEqual(value, other);
}
export function isEqualWith(value: any, other: any, customizer?: any) {
  return _.isEqualWith(value, other, customizer);
}
export function pickItem(array: any[], idx: number) {
  return array[idx];
}
export function timeVerify(currentTime: string, type: string, one: string, two?: string) {
  if (!type || !currentTime || !one) {
    throw new Error('缺少必要的参数');
  }
  const parseTime = (time: '当前' | Date | string): Date => {
    return time === '当前' ? new Date() : new Date(time);
  };

  const current = parseTime(currentTime).getTime();
  const oneTime = parseTime(one).getTime();
  const twoTime = two ? parseTime(two).getTime() : undefined;
  switch (type) {
    case '大于':
      return current > oneTime;
    case '小于':
      return current < oneTime;
    case '等于':
      return current === oneTime;
    case '之间':
      if (!twoTime) {
        throw new Error("当 'type' 为 '之间' 时，需要提供 '第四' 参数");
      }
      return current > oneTime && current < twoTime;
    default:
      throw new Error(`无效的 type 值: ${type}`);
  }
}
